package com.leon.android.ffmpeg;

import android.text.TextUtils;

import com.google.gson.Gson;

import java.net.URLConnection;

import java.lang.Exception;
/**
 * @time:2019/8/14 15:53
 * @author:Leon
 * @description:
 */
public class FFmpegHelper {
    private static final String TAG = "FFmpegHelper";
    static {
//        System.loadLibrary("avutil");
//        System.loadLibrary("swresample");
//        System.loadLibrary("avcodec");
//        System.loadLibrary("avformat");
//        System.loadLibrary("swscale");
//        System.loadLibrary("avfilter");
        System.loadLibrary("native-lib");
    }

    /**
     * 获取视频文件信息
     * @param path 文件路径
     */
    public static VideoInfo getVideoInfo(String path) {
        VideoInfo errorInfo = new VideoInfo();
        errorInfo.setFilePath(path);

//        if (!isVideoFile(path)){
//            errorInfo.setErrorMsg("This file is not a video!");
//            return errorInfo;
//        }

        String jsonStr = getVideoInfoNative(path);

        if (TextUtils.isEmpty(jsonStr)) {
            errorInfo.setErrorMsg("Video info is null!");
            return errorInfo;
        } else {
            try {
                VideoInfo info = new Gson().fromJson(jsonStr, VideoInfo.class);
                info.setRawString(jsonStr);
                return info;
            } catch (Exception e){
                errorInfo.setErrorMsg(jsonStr);
                e.printStackTrace();
                return errorInfo;
            }
        }
    }

    public static boolean isVideoFile(String path) {
        String mimeType = URLConnection.guessContentTypeFromName(path);
        return mimeType != null && mimeType.startsWith("video");
    }

    private static native String getVideoInfoNative(String url);
}
